package com.easylinkin.linkappapi.keywork.utils;

import com.easylinkin.linkappapi.keywork.dto.SafeTableDto;
import com.easylinkin.linkappapi.keywork.entity.KeyworkList;
import com.easylinkin.linkappapi.keywork.service.KeyworkListService;
import com.easylinkin.linkappapi.keywork.service.SafeTableService;
import com.easylinkin.linkappapi.keywork.vo.KeyworkListVo;
import com.easylinkin.linkappapi.keywork.vo.SafeTableTreeVo;
import com.easylinkin.linkappapi.keywork.vo.SafeTableVo;
import com.easylinkin.linkappapi.penetsecuremanage.service.IRailLinkappGridManagementInfoService;

import java.util.ArrayList;
import java.util.List;

public class CreateSafeTableTreeUtils {
    public static List<SafeTableTreeVo> createSafeTableTree(KeyworkListService keyworkListService,
                                                          SafeTableService safeTableService, Long gridId){
        List<SafeTableTreeVo> safeTableTreeVoList = new ArrayList<>();
        KeyworkList classSearch = new KeyworkList();
        classSearch.setLevel(1); //一级分类
        classSearch.setParentId(0L); //一级分类特征：上级id为0
        classSearch.setIsUsed(1); //正在使用的一级分类
        List<KeyworkListVo> classList = keyworkListService.selectList(classSearch);  //查询分类
        if (classList.size()>0){
            for (KeyworkList classify: classList){
                //封装第一层分类
                SafeTableTreeVo classifyVo = new SafeTableTreeVo();
                classifyVo.setId(classify.getId().toString()); //分类id
                classifyVo.setExpand(true);
                classifyVo.setTitle(classify.getContentName());
                classifyVo.setDataType("classify");
                classifyVo.setSafeTableTreeVoList(new ArrayList<>());
                safeTableTreeVoList.add(classifyVo);


                KeyworkList workSearch = new KeyworkList();
                workSearch.setIsUsed(1);
                workSearch.setLevel(2);
                workSearch.setParentId(classify.getId());
                List<KeyworkListVo> keyworkListList = keyworkListService.selectList(workSearch);  //查询分类下作业
                if (keyworkListList.size() > 0){ //查询表格
                    for (KeyworkList keywork:keyworkListList){
                        //
                        SafeTableTreeVo keyworkVo = new SafeTableTreeVo();
                        keyworkVo.setId(keywork.getId().toString()); //作业id
                        keyworkVo.setExpand(true);
                        keyworkVo.setTitle(keywork.getContentName());
                        keyworkVo.setDataType("keywork");
                        keyworkVo.setSafeTableTreeVoList(new ArrayList<>());
                        classifyVo.getSafeTableTreeVoList().add(keyworkVo);

                        SafeTableDto tableDtoSearch = new SafeTableDto();
                        tableDtoSearch.setKeyworkId(keywork.getId());
                        if (gridId !=null){
                            tableDtoSearch.setGridId(gridId);
                        }
                        List<SafeTableVo> safeTableVoList = safeTableService.selectList(tableDtoSearch); //查询作业下表格
                        if (safeTableVoList.size() > 0){
                            for (SafeTableVo tableVo : safeTableVoList){
                                SafeTableTreeVo safeTableTreeVo = new SafeTableTreeVo();
                                safeTableTreeVo.setId(tableVo.getId().toString()); //表格id
                                safeTableTreeVo.setExpand(true);
                                safeTableTreeVo.setTitle(tableVo.getTableName());
                                safeTableTreeVo.setDataType("safeTable");
                                keyworkVo.getSafeTableTreeVoList().add(safeTableTreeVo);
                            }

                        }
                    }

                }

            }


        }
        return safeTableTreeVoList;
    }
}
